# 商品发布

from base import build_driver, _YamlHelper
from page import Page
from page.background_login_page import BackgroundLoginPage


class AddGoodsPage(Page):
    addgoods_yaml = _YamlHelper().get_config_as_dict('niushop22\\page\\addgoods.yaml')['AddGoodsPage']

    def add_goods(self):
        '''
        后台登录
        :return:
        '''
        driver = self._driver
        loginpage = BackgroundLoginPage(driver)
        loginpage.back_login('admin', '123456')

    def user_text(self):
        '''
        登录后获取用户名文本
        :return:
        '''
        driver = self._driver
        user_name = driver.get_text(self.addgoods_yaml['GET_USERNAME_TEXT'])
        return user_name

    def list_text(self):
        '''
        获取商品列表文本
        :return:
        '''
        driver = self._driver
        goods_list = driver.get_text(self.addgoods_yaml['GET_GOODSLIST_TEXT'])
        return goods_list

    def type_goods(self):
        '''
        获取商品类型文本
        :return:
        '''
        driver = self._driver
        goods_type = driver.get_text(self.addgoods_yaml['GET_GOODSTYPE_TEXT'])
        return goods_type

    def property_text(self):
        '''
        获取商品属性页面基础信息文本
        :return:
        '''
        driver = self._driver
        driver.click(self.addgoods_yaml['PROPERTY_BUTTON'])
        simple_text = driver.get_text(self.addgoods_yaml['GET_SIMPLE_TEXT'])
        return simple_text

    def title_text(self):
        '''
        获取商品详情页面自定义标题文本
        :return:
        '''
        driver = self._driver
        driver.click(self.addgoods_yaml['CLICK_GOODS_BUTTON'])
        myself_text = driver.get_text(self.addgoods_yaml['GET_GOODSDETAIL_TEXT'])
        return myself_text

    def goods_picture(self):
        '''
        获取媒体设置页面商品图片文本
        :return:
        '''
        driver = self._driver
        driver.click(self.addgoods_yaml['MEDIASET_BUTTON'])
        picture_text = driver.get_text(self.addgoods_yaml['GET_GOODSPICTURE_TEXT'])
        return picture_text

    def click_goods(self):
        '''
        点击商品进入商品列表
        :return:
        '''
        driver = self._driver
        driver.click(self.addgoods_yaml['GOODS_BUTTON'])

    def add_button(self):
        '''
        点击添加商品按钮
        :return:
        '''
        driver = self._driver
        driver.click(self.addgoods_yaml['ADDGOODS_BUTTON'])

    def goods_message(self):
        '''
        填写商品基础信息页面
        :return:
        '''
        driver = self._driver
        # 输入商品名称
        driver.type(self.addgoods_yaml['GOODSNAME_WRITE'], 'iPhone12')
        # 选择分类
        driver.click(self.addgoods_yaml['CLASSIFY_SELECT'])
        # 休眠
        driver.forced_wait(1)
        # 切换面iframe
        driver.switch_to_frame(self.addgoods_yaml['GO_INTO_FRAME'])
        # 选择分类中子分类
        driver.click(self.addgoods_yaml['FIRST_SORT'])
        driver.click(self.addgoods_yaml['SECOND_SORT'])
        driver.click(self.addgoods_yaml['THIRD_SORT'])
        driver.click(self.addgoods_yaml['KEEPSORT_BUTTON'])
        # 退出最外层iframe
        driver.switch_to_default()
        # 休眠
        driver.forced_wait(1)
        # 扩展分类
        # driver.click('x,//*[@id="extend_name_category_box"]/div[2]/button')
        # driver.click('c,category-text')
        # driver.forced_wait(1)
        # driver.switch_to_frame('x,/html/body/div[1]/div/table/tbody/tr[2]/td/div/table/tbody/tr[2]/td[2]/div/iframe')        # driver.click('x,//*[@id="21"]/span[1]')
        # driver.click('x,//*[@id="22"]/span[1]')
        # driver.click('x,//*[@id="23"]/span[1]')
        # driver.click('x,//*[@id="next_Page"]')
        # driver.forced_wait(1)
        # driver.switch_to_default()
        # 促销语
        driver.type(self.addgoods_yaml['PROMOTION_WRITE'], '独家5G首发268G运行内存+1T容量')
        # 关键词
        driver.type(self.addgoods_yaml['KEYWORD_WRITE'], 'iPhone|苹果')
        # 商品单位
        driver.type(self.addgoods_yaml['GOODSUNIT_WRITE'], '台')
        # 滑动页面
        driver.drag_scrollbar(400)
        # 选择标签
        driver.click(self.addgoods_yaml['LABEL_BUTTON'])
        driver.click(self.addgoods_yaml['CLICK_LABELCONTENT'])
        driver.click(self.addgoods_yaml['CLICK_LABEL'])
        # #选择品牌
        driver.click(self.addgoods_yaml['GOODS_BRAND'])
        driver.click(self.addgoods_yaml['CLICK_BRANDLABEL'])
        # 选择供货商
        driver.click(self.addgoods_yaml['SUPPLIER_SELECT'])
        driver.click(self.addgoods_yaml['CLICK_SUPPLIER_LABEL'])
        # 基础量
        driver.type(self.addgoods_yaml['SMIPLE_NUM_WRITE'], '9999')
        # 基础点击量
        driver.type(self.addgoods_yaml['SMIPLE_NUM_CLICK_WRITE'], '99999')
        # 基础分享量
        driver.type(self.addgoods_yaml['SHARE_NUM_WRITE'], '999999')
        # 商品编码
        driver.type(self.addgoods_yaml['GOODS_CONDING_WRITE'], 'ip12345')
        # 生产日期
        driver.type(self.addgoods_yaml['PRODUCED_DATE_WRITE'], '2019-04-03')
        # 保质期天数
        driver.type(self.addgoods_yaml['QUALITY_DATE_WRITE'], '160')
        # 总库存
        driver.type(self.addgoods_yaml['REPERTORY_NUM'], '9999999')
        # 预警库存
        driver.type(self.addgoods_yaml['WARING_REPERTORY_NUM'], '100')
        # 滑动页面
        driver.drag_scrollbar(1000)
        # 库存显示
        driver.click(self.addgoods_yaml['CLICK_REPERTORY'])
        # 是否上架
        driver.click(self.addgoods_yaml['PUTAWAY_GOODS'])
        # 商品所在地
        driver.click(self.addgoods_yaml['PROVINCE_GOODS'])
        driver.click(self.addgoods_yaml['CLICK_PROVINCE'])
        driver.forced_wait(1)
        driver.click(self.addgoods_yaml['CITY_GOODS'])
        driver.click(self.addgoods_yaml['CLICK_CITY'])
        # 市场价格
        driver.type(self.addgoods_yaml['MARKET_PRICE'], '8999')
        # 销售价格
        driver.type(self.addgoods_yaml['SALE_PRICE'], '12999')
        # 成本价格
        driver.type(self.addgoods_yaml['COST_PRICE'], '6999')
        # 滑动页面
        driver.drag_scrollbar(1300)
        # 阶梯优惠
        driver.click(self.addgoods_yaml['DISCOUNT_LADDER'])
        driver.type(self.addgoods_yaml['DISCOUNT_NUM'], '10')
        driver.type(self.addgoods_yaml['DISCOUNT_PRICE'], '500')
        # 运费和计价方式点击
        driver.click(self.addgoods_yaml['FREIGHT_COUNT'])
        driver.click(self.addgoods_yaml['CLICK_FREIGHT_COUNT'])
        # 商品重量
        driver.type(self.addgoods_yaml['GOODS_WEIGHT'], '2')
        # 商品体积
        driver.type(self.addgoods_yaml['GOODS_VOLUME'], '10')
        # 页面滑动
        driver.drag_scrollbar(1500)
        # 物流公司
        driver.click(self.addgoods_yaml['LOGISTICS_COMPANY'])
        driver.click(self.addgoods_yaml['CLICK_LOGISTICS_COMPANY'])
        # 每人限购
        driver.type(self.addgoods_yaml['BUY_IMPOSE_WRITE'], '2')
        # 至少购买数量
        driver.type(self.addgoods_yaml['ATLEAST_BUY_NUM_WRITE'], '1')
        # 滑动页面
        driver.drag_scrollbar(1700)
        # 是否预售
        driver.click(self.addgoods_yaml['CLICK_WARNING'])
        # 预售金额
        driver.type(self.addgoods_yaml['WARNINF_PRICE_WRITE'], '1000')
        # 预售发货方式
        driver.click(self.addgoods_yaml['WARNING_DELIVER_WAY'])
        # 预售发货时间
        driver.type(self.addgoods_yaml['WARNING_DELIVERY_DATE_WRITE'], '2019-04-20')
        # 最大可用积分
        driver.type(self.addgoods_yaml['MAXI_INTEGRAL_WRITE'], '5000')
        # 页面滑动
        driver.drag_scrollbar(10000)
        # 积分兑换设置
        driver.click(self.addgoods_yaml['CLICK_INTEGRAL_EXCHANGE'])
        # 所需兑换积分
        driver.type(self.addgoods_yaml['INTEGRAL_EXCHANGE'], '30000')
        # 购买赠送积分
        driver.click(self.addgoods_yaml['BUY_INTEGRAL'])
        driver.type(self.addgoods_yaml['INTEGRAL_WRITE'], '1000')
        # 折扣设置普通会员折扣
        driver.type(self.addgoods_yaml['MEMBER_DISCOUNT_WRITE'], '98')
        # 价格保留方式
        driver.click(self.addgoods_yaml['CLICK_KEEP_PRICE'])

    def goods_property(self):
        '''
        填写商品属性信息页面
        :return:
        '''
        driver = self._driver
        # 商品属性
        driver.click(self.addgoods_yaml['CLICK_GOODS_PROPERTY'])
        driver.click(self.addgoods_yaml['CLICK_GOODS_TYPE'])
        driver.type(self.addgoods_yaml['GOODS_PROPERTY_WRITE'], 'iPhone')
        # 滑动页面
        driver.drag_scrollbar(0)

    def media_set(self):
        '''
        填写媒体设置页面
        :return:
        '''
        driver = self._driver
        # 添加图片
        driver.type(self.addgoods_yaml['ADD_PICTURE'], 'c:\\HHH\\Penguins.jpg')
        # 添加视频
        driver.type(self.addgoods_yaml['ADD_VIDEO'],
                    'https://www.apple.com/105/media/cn/iphone-xr/2018/e7f073a7-e8ae-4c18-937b-91d8939fcadd/films/design/iphone-xr-design-tpl-cn-2018_1280x720h.mp4')
        driver.drag_scrollbar(0)

    def goods_details(self):
        '''
        填写商品详情页面
        :return:
        '''
        driver = self._driver
        # 商品详情
        driver.click(self.addgoods_yaml['CLICK_GOODS_DETAILS'])
        driver.switch_to_frame(self.addgoods_yaml['GO_TO_FRAME'])
        driver.type(self.addgoods_yaml['GOODS_DETAILS_WRITE'], '高贵的人在使用iPhone')
        driver.switch_to_default()
        # 保存
        driver.click(self.addgoods_yaml['KEEPGOODS_BUTTON'])

    def close(self):
        '''
        关闭浏览器
        :return:
        '''
        driver = self._driver
        driver.quit()


if __name__ == '__main__':
    driver = build_driver('http://www.localhost/niushop/admin', 'c')
    addgoodspage = AddGoodsPage(driver)
